//
//  SampleTableView.m
//  SDKSample
//
//  Created by SuSmile on 14-12-23.
//  Copyright (c) 2014年 Zhu‘s Game House. All rights reserved.
//

#import "SampleTableView.h"

#pragma mark - SampleTable数据单元
@implementation SampleTableListItem

#pragma mark - 初始化
- (id)init
{
    self = [super init];
    return self;
}

- (id)initWithTitle:(NSString *)title explain:(NSString *)explain
{
    self = [self initWithTitle:title explain:explain controllerData:nil];
    return self;
}

- (id)initWithTitle:(NSString *)title explain:(NSString *)explain controllerData:(NSString *)controllerData;
{
    self = [super init];
    if (self) {
        _title = title;
        _explain = explain;
        _controllerData = controllerData;
    }
    return self;
}

- (UIViewController *)controller
{
    if (!_controller) {
        if (_controllerData) {
            UIViewController *productsList = [[[NSBundle mainBundle] loadNibNamed:_controllerData owner:nil options:nil] lastObject];
            if (productsList){
                _controller = productsList;
            }
        }
    }
    return _controller;
}

@end

#pragma mark - Sample表格
@implementation SampleTableView

#pragma mark - 初始化
/*! @brief 故事板初始化 */
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self initValue:nil];
    }
    return self;
}

- (id)init
{
    self = [super init];
    if (self) {
        [self initValue:nil];
    }
    return self;
}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initValue:nil];
    }
    return self;
}
- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    self = [self initWithFrame:frame style:style data:nil];
    return self;
}
- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style data:(NSArray *)dataList
{
    self = [super initWithFrame:frame style:style];
    if (self) {
        [self initValue:dataList];
    }
    return self;
}

- (void)initValue:(NSArray *)dataList
{
    if (dataList) {
        _dataList = [[NSMutableArray alloc] init];
    } else {
        _dataList = [[NSMutableArray alloc] initWithArray:dataList];
    }
    self.dataSource = self;
    self.delegate = self;
}

#pragma mark - 属性设置
- (void)setDataList:(NSMutableArray *)dataList
{
    _dataList = dataList;
}

#pragma mark - 取值方法
- (SampleTableListItem *)dataItemWithRow:(NSUInteger)row
{
    if ([[self.dataList objectAtIndex:row] isKindOfClass:[SampleTableListItem class]]) {
        return [self.dataList objectAtIndex:row];
    }
    return nil;
}
- (NSString *)dataTitleWithRow:(NSUInteger)row
{
    SampleTableListItem *data = [self dataItemWithRow:row];
    if (data && data.title){
        return data.title;
    }
    return @"没有标题";
}
- (NSString *)dataExplainWithRow:(NSUInteger)row
{
    SampleTableListItem *data = [self dataItemWithRow:row];
    if (data && data.explain){
        return data.explain;
    }
    return @"没有说明";
}
- (UIViewController *)dataControllerWithRow:(NSUInteger)row
{
    SampleTableListItem *data = [self dataItemWithRow:row];
    return data.controller;
}

#pragma mark - 表格显示及操作
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dataList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellWithIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellWithIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellWithIdentifier];
    }
    NSUInteger row = [indexPath row];
    cell.textLabel.text = [self dataTitleWithRow:row];
    cell.detailTextLabel.text = [self dataExplainWithRow:row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UIViewController *controller = [self dataControllerWithRow:[indexPath row]];
    if (controller) {
        for (UIView* next = [self superview]; next; next = next.superview) {
            UIResponder *nextResponder = [next nextResponder];
            if ([nextResponder isKindOfClass:[UIViewController class]]) {
                controller.hidesBottomBarWhenPushed = YES;
                [((UIViewController *)nextResponder).navigationController pushViewController:controller animated:YES];
                break;
            }
        }
    } else {
        NSString *msg = [[NSString alloc] initWithFormat:@"你选择的是:%@",[self dataTitleWithRow:[indexPath row]]];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"不存在Controller" message:msg delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }

}

@end
